package com.ll.domain.user.event.consumer;

import com.ll.app.api.req.UpdateResultPkReqDTO;
import com.ll.domain.pk.dto.PkDTO;
import com.ll.domain.pk.enums.PkResultEnum;
import com.ll.domain.pk.event.producer.AddPkEvent;
import com.ll.domain.pk.event.producer.UpdateResultPkEvent;
import com.ll.domain.user.base.UserBaseService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * pk 记录添加
 * @author WuHen
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class UpdateResultPkEventToUserListener implements ApplicationListener<UpdateResultPkEvent> {

    private final UserBaseService userBaseService;

    @Async
    @Override
    public void onApplicationEvent(UpdateResultPkEvent resultPkEvent) {
        PkDTO pkDTO = resultPkEvent.getPkDTO();
        log.info("接收到 PK处理结果 事件  id ：{}", pkDTO.getId());

        if(pkDTO.getResult() == PkResultEnum.发起人获胜){

            userBaseService.addIntegral(pkDTO.getSourceId(), pkDTO.getIntegral() * 2);

        }else  if(pkDTO.getResult() == PkResultEnum.目标人获胜){

            userBaseService.addIntegral(pkDTO.getTargetId(), pkDTO.getIntegral() * 2);

        }

        log.info("接收到 PK处理结果 事件  id ：{} 处理成功", pkDTO.getId());
    }
}
